package com.lapelife.backend.controller;

import com.lapelife.backend.common.ResponseResult;
import com.lapelife.backend.model.request.PostRequest;
import com.lapelife.backend.service.PostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("admin/post")
@Api(tags = "文章管理")
public class PostManagerController {

    private PostService postService;

    @Autowired
    public void setPostService(PostService postService) {
        this.postService = postService;
    }

    @ApiOperation("爬取CSDN添加文章")
    @PostMapping("/crawler/add")
    @PreAuthorize("hasAuthority('post:crawler:add')")
    public ResponseResult<Integer> addPostToDB(@RequestBody PostRequest postRequest) {
        return postService.addPost(postRequest);
    }

    @ApiOperation("添加文章")
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('post:custom:add')")
    public ResponseResult<Integer> addPostByCustom(@RequestBody PostRequest postRequest) {
        return postService.addPostByCustom(postRequest);
    }

    @ApiOperation("修改文章")
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('post:custom:update')")
    public ResponseResult<Integer> updatePost(@RequestBody PostRequest postRequest) {
        return postService.updatePost(postRequest);
    }

    @ApiOperation("删除文章")
    @PutMapping("/delete")
    @PreAuthorize("hasAuthority('post:custom:delete')")
    public ResponseResult<Integer> deletePost(@RequestBody PostRequest postRequest) {
        return postService.deletePost(postRequest.getIdList());
    }
}
